﻿using System;
using System.Runtime.InteropServices;
using System.Threading.Tasks;
using Dpx.Models;
using Dpx.Services;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using Xamarin.Essentials;

namespace Dpx.ViewModels {
    /// <summary>
    /// 今日推荐页ViewModel
    /// </summary>
    public class TodayPageViewModel : ViewModelBase {
        /******** 构造函数 ********/

        /// <summary>
        /// 今日图片服务。
        /// </summary>
        private readonly ITodayImageService _todayImageService;

        /// <summary>
        /// 今日诗词服务。
        /// </summary>
        private readonly ITodayPoetryService _todayPoetryService;

        /// <summary>
        /// 浏览器服务
        /// </summary>
        private readonly IBrowserService _browserService;


        private readonly IRootNavigationService _rootNavigationService;
        private readonly IContentNavigationService _contentNavigationService;

        /// <summary>
        /// 今日推荐页ViewModel
        /// </summary>
        /// <param name="todayImageService">今日图片服务。</param>
        /// <param name="todayPoetryService">今日诗词服务。</param>
        /// <param name="browserService">浏览器服务</param>
        /// <param name="contentNavigationService">内容导航服务。</param>
        public TodayPageViewModel(ITodayImageService todayImageService,
            ITodayPoetryService todayPoetryService, IBrowserService browserService,
            IContentNavigationService contentNavigationService, IRootNavigationService rootNavigationService) {
            _todayImageService = todayImageService;
            _todayPoetryService = todayPoetryService;
            _browserService = browserService;
            _contentNavigationService = contentNavigationService;
            _rootNavigationService = rootNavigationService;
        }

        /******** 绑定属性 ********/

        /// <summary>
        /// 今日图片。
        /// </summary>
        public TodayImage TodayImage {
            get => _todayImage;
            set => Set(nameof(TodayImage), ref _todayImage, value);
        }

        /// <summary>
        /// 今日图片。
        /// </summary>
        private TodayImage _todayImage;

        /// <summary>
        /// 今日诗词。
        /// </summary>
        public TodayPoetry TodayPoetry {
            get => _todayPoetry;
            set => Set(nameof(TodayPoetry), ref _todayPoetry, value);
        }

        /// <summary>
        /// 今日诗词。
        /// </summary>
        private TodayPoetry _todayPoetry;

        /// <summary>
        /// 今日诗词已加载。
        /// </summary>
        public bool TodayPoetryLoaded {
            get => _todayPoetryLoaded;
            set =>
                Set(nameof(TodayPoetryLoaded), ref _todayPoetryLoaded, value);
        }

        /// <summary>
        /// 今日诗词已加载。
        /// </summary>
        private bool _todayPoetryLoaded;

        /// <summary>
        /// 正在加载今日诗词。
        /// </summary>
        public bool TodayPoetryLoading {
            get => _todayPoetryLoading;
            set =>
                Set(nameof(TodayPoetryLoading), ref _todayPoetryLoading, value);
        }

        /// <summary>
        /// 正在加载今日诗词。
        /// </summary>
        private bool _todayPoetryLoading;

        /******** 绑定命令 ********/

        /// <summary>
        /// 搜索命令。
        /// </summary>
        public RelayCommand QueryCommand =>
            _queryCommand ??= new RelayCommand(async () =>
                await QueryCommandFunction());

        /// <summary>
        /// 搜索命令。
        /// </summary>
        private RelayCommand _queryCommand;

        public async Task QueryCommandFunction() {
            var poetryQuery = new PoetryQuery {
                Name = TodayPoetry.Name,
                AuthorName = TodayPoetry.AuthorName
            };
            await _rootNavigationService.NavigateToAsync(
                RootNavigationConstant.QueryPage, poetryQuery);
        }

        /// <summary>
        /// 页面显示命令。
        /// </summary>
        public RelayCommand PageAppearingCommand =>
            _pageAppearingCommand ??=
                new RelayCommand(PageAppearingCommandFunction);

        public void PageAppearingCommandFunction() {
            Task.Run(async () => {
                TodayPoetryLoading = true;
                TodayPoetryLoaded = false;
                TodayPoetry = await _todayPoetryService.GetTodayPoetryAsync();
                TodayPoetryLoading = false;
                TodayPoetryLoaded = true;
            });

            Task.Run(async () => {
                TodayImage = await _todayImageService.GetTodayImageAsync();
                var checkUpdate = await _todayImageService.CheckUpdateAsync();
                if (checkUpdate.HasUpdate) {
                    TodayImage = checkUpdate.TodayImage;
                }
            });
        }

        /// <summary>
        /// 页面显示命令。
        /// </summary>
        private RelayCommand _pageAppearingCommand;

        /// <summary>
        /// 查看详细命令。
        /// </summary>
        public RelayCommand ShowDetailCommand =>
            _showDetailCommand ??=
                new RelayCommand(async () =>
                    await ShowDetailCommandFunction());

        /// <summary>
        /// 查看详细命令。
        /// </summary>
        private RelayCommand _showDetailCommand;

        public async Task ShowDetailCommandFunction() =>
            await _contentNavigationService.NavigateToAsync(
                ContentNavigationConstant.TodayDetailPage);

        /// <summary>
        /// 今日诗词命令。
        /// </summary>
        public RelayCommand JinrishiciCommand =>
            _jinrishiciCommand ??=
                new RelayCommand(async () => await JinrishiciCommandFunction());

        public async Task JinrishiciCommandFunction() => await _browserService.OpenAsync("https://www.jinrishici.com");

        /// <summary>
        /// 今日诗词命令。
        /// </summary>
        private RelayCommand _jinrishiciCommand;

        /// <summary>
        /// 版权信息命令。
        /// </summary>
        public RelayCommand CopyrightCommand =>
            _copyrightCommand ??=
                new RelayCommand(async () => await CopyrightCommandFunction());

        public async Task CopyrightCommandFunction() =>
            await _browserService.OpenAsync(TodayImage.CopyrightLink);

        /// <summary>
        /// 版权信息命令。
        /// </summary>
        private RelayCommand _copyrightCommand;
    }
}